package org.lansir.af.reminder;

import java.util.Calendar;

import org.lansir.af.R;
import org.lansir.af.database.DataRowActivity;
import org.lansir.af.database.DataTable;
import org.lansir.af.database.Database;
import org.lansir.af.ui.CommonActivity;
import org.lansir.af.ui.ScheduleActivityFragment;
import org.lansir.af.util.Prefs;

import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * 闹钟到达时的窗口
 * @author 蓝智宏
 *
 */
public class AlarmArrivedDialog extends CommonActivity {

	private NotificationManager mNotificationManager;

	//private ImageView ivAlarmArrivedDialogHeaderImage;
	private TextView tvAlarmArrivedDialogSubject;
	private TextView tvAlarmArrivedDialogContent;
	private TextView tvAlarmArrivedDialogTime;
	private Button btnOpen;
	private Button btnClose;
	private Button btnSnooze;

	private long lRowId;
	private Prefs prefs;

	private AlarmSound alarmSound;
	private DataTable dataTable;
	private DataRowActivity dataRowActivity;

	@Override
	public void onCreate(Bundle icicle) {

		super.onCreate(icicle);
		init(R.layout.alarm_received_dialog);

	}

	@Override
	protected void initStates() {
		getIntentDataFromBundle();
		setTitle(utils.getResStr(R.string.titleAlarmDialog));
		this.dataRowActivity = new DataRowActivity(this.userdb);
		this.dataTable = new DataTable(this.dataRowActivity);
		this.dataTable.getRowDataForEdit(lRowId);
		this.tvAlarmArrivedDialogSubject.setText(dataRowActivity.getSubject());
		this.tvAlarmArrivedDialogContent.setText(dataRowActivity.getLocation());
		Calendar startDate = dataRowActivity.getStartDate();
		this.tvAlarmArrivedDialogTime.setText(ScheduleActivityFragment.pad(startDate.get(Calendar.HOUR_OF_DAY)) + ":" + ScheduleActivityFragment.pad(startDate.get(Calendar.MINUTE)));
		alarmSound.play();
	}

	@Override
	protected void initViews() {
		alarmSound = new AlarmSound(this);
		prefs = new Prefs(this);
		mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		//ivAlarmArrivedDialogHeaderImage = (ImageView) findViewById(R.id.ivAlarmArrivedDialogTopHeaderImage);
		tvAlarmArrivedDialogSubject = (TextView)findViewById(R.id.tvAlarmArrivedDialogSubject);
		tvAlarmArrivedDialogContent = (TextView) findViewById(R.id.tvAlarmArrivedDialogLocation);
		tvAlarmArrivedDialogTime = (TextView)findViewById(R.id.tvAlarmArrivedDialogTime);

		btnOpen = (Button) findViewById(R.id.btnAlarmReceivedDialogBottomOpen);
		btnSnooze = (Button) findViewById(R.id.btnAlarmReceivedDialogBottomSnooze);
		btnClose = (Button) findViewById(R.id.btnAlarmReceivedDialogBottomClose);

		btnOpen.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mNotificationManager.cancel(AlarmReminder.iNotifyID);
				if(dataRowActivity.getSnooze() == 1){
					dataRowActivity.setActionClear(SnoozeAction.convertToInt(SnoozeAction.Clear));
				}
				dataTable.updateData(false, dataRowActivity.getActivityId());
				openDataForEdit();
				AlarmArrivedDialog.this.finish();


			}

		});
		
		btnSnooze.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				if(dataRowActivity.getSnooze() == 0){
					dataRowActivity.setSnooze(1);
					dataRowActivity.setSnoozeCount(1);
					dataRowActivity.setActionClear(SnoozeAction.convertToInt(SnoozeAction.Snooze));
				}else{
					if(dataRowActivity.getSnoozeCount() <= prefs.iSnoozeCount)
						dataRowActivity.setSnoozeCount(dataRowActivity.getSnoozeCount() + 1);
				}
				Database.Result result = Database.Result.Success;
				
				result = dataTable.updateData(false, dataRowActivity.getActivityId());//dataTable.updateValues(dataRowActivity.getActivityId());
				Log.v("ActivityFairy", "Affect Result:" + result);
				AlarmArrivedDialog.this.finish();
			}	
		});

		btnClose.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if(dataRowActivity.getSnooze() == 1){
					dataRowActivity.setActionClear(SnoozeAction.convertToInt(SnoozeAction.Clear));
				}
				dataTable.updateData(false, dataRowActivity.getActivityId());
				AlarmArrivedDialog.this.finish();
			}

		});
	}


	private void getIntentDataFromBundle() {
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			lRowId = bundle.getLong("ROWID");
			bundle.clear();
		}

	}

	public void openDataForEdit() {
		bundleOtherDataStartup.clear();

		openActivityForResult(0,
					"org.lansir.af.intent.action.ACTION_MODE_VIEW_ACTIVITYINFO",
					lRowId);
		
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(alarmSound.isPlaying())
			alarmSound.finalize();
	}

	
	
}
